<?php
class AdminUserController extends Zendvn_Controller_Action{
	
    //Mang tham so nhan duoc o moi action
    protected $_arrParam;

    //Duong dan cua controller
    protected $_currentController;
    
    //Duong dan cua Action chinh
    protected $_actionMain;
	
    //Thong so phan trang
    protected $_paginator = array(
                                'ItemCountPerPage' => 5,
                                'PageRange' => 3,
                                );
	protected $_namespace;


    public function init() {
        //Mang tham so nhan duoc o moi action
        $this->_arrParam    =   $this->_request->getParams();
        
        //Duong dan cua controller
        $this->_currentController   =   '/' . $this->_arrParam['module'] 
                                      . '/' . $this->_arrParam['controller'];
        
        //Duong dan cua Action chinh
        $this->_actionMain  =   $this->_currentController . '/index';
        
        $this->_paginator['currentPage'] = $this->_request->getParam('page', 1);
        $this->_arrParam['paginator'] = $this->_paginator;

        //Luu cac du lieu filter vao SESSION
        //Dat ten SESSION
        $this->_namespace  = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
        $ssFilter = new Zend_Session_Namespace($this->_namespace);
//	$ssFilter->unsetAll();
        if(empty($ssFilter->col)){
                $ssFilter->keywords = '';
                $ssFilter->col = 'u.id';
                $ssFilter->order = 'DESC';
                $ssFilter->group_id = 0;
        }

        $this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
        $this->_arrParam['ssFilter']['col'] = $ssFilter->col;
        $this->_arrParam['ssFilter']['order'] = $ssFilter->order;
        $this->_arrParam['ssFilter']['group_id'] = $ssFilter->group_id;
		
        //Truyen ra view
        $this->view->arrParam   =   $this->_arrParam;
        $this->view->currentController  =   $this->_currentController ;
        $this->view->actionMain  =   $this->_actionMain;
        
        $template_path  =   TEMPLATE_PATH . "/admin/system";
        $this->loadTemplate($template_path , 'template.ini', 'template');
    }
    public function indexAction() {
        $this->view->Title = 'Member :: User manager :: List';
        //Thay doi title
        $this->view->headTitle($this->view->Title, true);
        
        $tblUser   = new Default_Model_Users();
        $this->view->Items  = $tblUser->listItem($this->_arrParam, array('task'=>'admin-list'));
        
        $tblGroup   = new Default_Model_UserGroup();
        $this->view->slbGroup  = $tblGroup->itemInSelectBox();
	       
        // Phan trang
        $totalItem = $tblUser->countItem($this->_arrParam);

        $paginator	= new Zendvn_Paginator();
        $this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
    }
    
    public function filterAction() {
        $ssFilter = new Zend_Session_Namespace($this->_namespace);
        if($this->_arrParam['type'] == 'search'){
                if($this->_arrParam['key'] == 1){
                        $ssFilter->keywords = trim($this->_arrParam['keywords']);
                }else{
                        $ssFilter->keywords = '';
                }
        }
        if($this->_arrParam['type'] == 'order'){
                $ssFilter->col = trim($this->_arrParam['col']);
                $ssFilter->order = trim($this->_arrParam['by']);
        }
        if($this->_arrParam['type'] == 'group'){
                $ssFilter->group_id = trim($this->_arrParam['group_id']);
        }
        $this->_redirect($this->_actionMain);
        $this->_helper->viewRenderer->setNoRender();
    }
	
    public function addAction() {
        $this->view->Title = 'Member :: User manager :: Add New';
        //Thay doi title
        $this->view->headTitle($this->view->Title, true);
        
        $tblGroup   = new Default_Model_UserGroup();
        $this->view->slbGroup  = $tblGroup->itemInSelectBox();
        
        if($this->_request->isPost()){
            
            $validator = new Default_Form_ValidateUser($this->_arrParam, $options);
            if($validator->isError() == true){
                $this->view->messageError = $validator->getMessageError();
                $this->view->Item = $validator->getData();
            }else{
                $tblUser = new Default_Model_Users();
                $arrParam = $validator->getData(array('upload'=>true));
                $tblUser->saveItem($arrParam, array('task'=>'admin-add'));
                $this->_redirect($this->_actionMain);
            }
        }
    }
    
    public function infoAction() {
        $this->view->Title = 'Member :: User manager :: Infomation';
        $this->view->headTitle($this->view->Title, true);
        $tblUsers   =   new Default_Model_Users();
        $this->view->Item   =   $tblUsers->getItem($this->_arrParam, array('task'=>'admin-info'));
    }
    
    public function editAction() {
        $this->view->Title = 'Member :: User manager :: Edit';
        $this->view->headTitle($this->view->Title, true);
        
        $tblGroup   = new Default_Model_UserGroup();
        $this->view->slbGroup  = $tblGroup->itemInSelectBox();
        
        $tblUsers   =   new Default_Model_Users();
        $this->view->Item   =   $tblUsers->getItem($this->_arrParam, array('task'=>'admin-info'));
        
        if($this->_request->isPost()){
            
            $validator = new Default_Form_ValidateUser($this->_arrParam, $options);
            if($validator->isError() == true){
                $this->view->messageError = $validator->getMessageError();
                $this->view->Item = $validator->getData();
            }else{
                $tblUser = new Default_Model_Users();
                $arrParam = $validator->getData(array('upload'=>true));
                $tblUser->saveItem($arrParam, array('task'=>'admin-edit'));
                $this->_redirect($this->_actionMain);
            }
        }
    }
    
    public function deleteAction() {
        $this->view->Title = 'Member :: User manager :: Delete';
        $this->view->headTitle($this->view->Title, true);
        if($this->_request->isPost()){
            $tblGroup   =   new Default_Model_Users();
            $tblGroup->deleteItem($this->_arrParam, array('task'=>'admin-delete'));
            $this->_redirect($this->_actionMain);
        }
    }
    
    public function statusAction() {
        $tblUser   =   new Default_Model_Users();
        $tblUser->changeStatus($this->_arrParam);
        $this->_redirect($this->_actionMain);
        
        $this->_helper->viewRenderer->setNoRender();
    }
	
    public function multiDeleteAction() {
        if($this->_request->isPost()){
			$tblUser   =   new Default_Model_Users();
			$tblUser->deleteItem($this->_arrParam, array('task'=>'admin-multi-delete'));
        }
		$this->_redirect($this->_actionMain);
        $this->_helper->viewRenderer->setNoRender();
    }
	
	public function sortAction() {
		if($this->_request->isPost()){
			$tblGroup   =   new Default_Model_UserGroup();
			$tblGroup->sortItem($this->_arrParam, array('task'=>'admin-sort'));
        }
		$this->_redirect($this->_actionMain);
		$this->_helper->viewRenderer->setNoRender();
	}

}